package com.hyg.module_report.ui.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.hyg.lib_common.DataModel.ReceiveData;
import com.hyg.lib_common.DataUtil.UserSPUtils;
import com.hyg.lib_common.constant.FileConstants;
import com.hyg.module_report.R;
import com.hyg.module_report.adapter.V2AdviceContentTextAdapter;
import com.hyg.module_report.adapter.V2HealthAdviceAdapter;
import com.hyg.module_report.databinding.FragmentHealthAdviceV2DetailBinding;
import com.hyg.module_report.model.V2AdviceDetailData;
import com.hyg.module_report.model.V2HealthAdviceData;
import com.hyg.module_report.ui.activity.HealthReportV2Activity;

import java.util.ArrayList;
import java.util.List;

import static android.view.View.GONE;

public class HealthAdviceV2DetailFragment extends Fragment {
    HealthReportV2Activity Activity;
    FragmentHealthAdviceV2DetailBinding binding;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = FragmentHealthAdviceV2DetailBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();
        Activity = (HealthReportV2Activity) getActivity();
        UserSPUtils.clearHealthAdviceInfo(Activity);
    }

    @Override
    public void onResume() {
        super.onResume();
        init();
    }

    public void init() {
        if(Activity.adviceReceiveData!=null){
            showHealthAdviceView(Activity.adviceReceiveData);
        }
    }


    /**
     * description: 显示报告详细信息
     * @param  receiveData 服务器返回的报告详细数据
     * @author visitant
     * @time 2021/1/6 11:46
     */
    public void showHealthAdviceView(ReceiveData receiveData) {
        if (receiveData.getCode().equals("500")) {
            Toast.makeText(Activity, "未查到养生建议信息！", Toast.LENGTH_SHORT).show();
        } else if (receiveData.getCode().equals("200")) {
            V2HealthAdviceData data = JSONObject.parseObject(receiveData.getData(), V2HealthAdviceData.class);
            binding.yangshengYuanze.setText("\u3000\u3000"+data.getPrinciple().get(0).getPrinciple());
            showYinShiView(data);
            showYunDongView(data);
            showZuYuView(data);
            showliLiaoView(data);
            showQiJieView(data);
        } else {

        }
    }

    /**
     * description: 显示饮食相关数据
     * @param  data 报告的详细数据
     * @author visitant
     * @time 2021/1/6 11:46
     */
    public void showYinShiView(V2HealthAdviceData data) {
        List<V2AdviceDetailData> diet=data.getDiet();
        ArrayList<V2AdviceDetailData> shicai_list=new ArrayList();
        ArrayList<V2AdviceDetailData> yinshi_list=new ArrayList();
        ArrayList<V2AdviceDetailData> chayin_list=new ArrayList();
        ArrayList<V2AdviceDetailData> yaoshan_list=new ArrayList();
        for(V2AdviceDetailData DetailData :diet ){
            if(DetailData.getCategoryName().equals("饮食指导")){
                Glide.with(this).load(DetailData.getImageUrl()).error(R.mipmap.tongue_default).into(binding.ivYangshengZhidao);
                binding.tvZhidaoImagehint.setText(DetailData.getTips());
                binding.yinshiYuanze.setText("\u3000\u3000"+DetailData.getContent());
            }else if(DetailData.getCategoryName().equals("食材推荐")){
                shicai_list.add(DetailData);
            }else if(DetailData.getCategoryName().equals("一般饮食")){
                yinshi_list.add(DetailData);
            }else if(DetailData.getCategoryName().equals("茶饮")){
                chayin_list.add(DetailData);
            }else if(DetailData.getCategoryName().equals("药膳")){
                yaoshan_list.add(DetailData);
            }
        }
        if(shicai_list==null || shicai_list.size()==0 ){
            binding.tvShicaituijianTitle.setVisibility(GONE);
            binding.rcShicaituijian.setVisibility(GONE);
        }else {
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            binding.rcShicaituijian.setLayoutManager(layoutManager);
            V2AdviceContentTextAdapter adapter = new V2AdviceContentTextAdapter(shicai_list);
            binding.rcShicaituijian.setAdapter(adapter);
        }

        diet.clear();
        for(V2AdviceDetailData DetailData:yinshi_list){
            diet.add(DetailData);
        }
        for(V2AdviceDetailData DetailData:chayin_list){
            diet.add(DetailData);
        }
        for(V2AdviceDetailData DetailData:yaoshan_list){
            diet.add(DetailData);
        }
        LinearLayoutManager layoutManager1 = new LinearLayoutManager(getActivity());
        layoutManager1.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rcYinshituijian.setLayoutManager(layoutManager1);
        V2HealthAdviceAdapter yinshi_adapter = new V2HealthAdviceAdapter(diet);
        binding.rcYinshituijian.setAdapter(yinshi_adapter);

        JSONArray array= JSONArray.parseArray(JSON.toJSONString(diet));
        UserSPUtils.putUserString(getActivity(), FileConstants.Yinshi_data,JSON.toJSONString(array));
        UserSPUtils.putUserString(getActivity(), FileConstants.HasReport,"true");
    }



    /**
     * description: 显示运动养生信息
     * @param  data 报告的详细数据
     * @author visitant
     * @time 2021/1/6 11:48
     */
    public void showYunDongView(V2HealthAdviceData data) {
        List<V2AdviceDetailData> sport=data.getSport();
        ArrayList<V2AdviceDetailData> weiyundong_list=new ArrayList();
        ArrayList<V2AdviceDetailData> gongfa_list=new ArrayList();
        for(V2AdviceDetailData DetailData :sport ){
            if(DetailData.getCategoryName().equals("运动原则")){
                binding.tvYundongqiangdu.setText("您的各运动强度适宜心率如上图所示: \n\u3000\u3000根据人群运动原则，建议您："+DetailData.getPrinciple());
            }else if(DetailData.getCategoryName().equals("运动提示")){
                if(TextUtils.isEmpty(DetailData.getTips())){
                    binding.rlYundongNotice.setVisibility(GONE);
                }else {
                    binding.tvYundongNotice.setVisibility(View.VISIBLE);
                    binding.tvYundongNotice.setText("\u3000\u3000" + DetailData.getTips());
                }
            }
            else if(DetailData.getCategoryName().equals("微运动")){
                weiyundong_list.add(DetailData);
            }else if(DetailData.getCategoryName().equals("中医功法")){
                gongfa_list.add(DetailData);
            }
        }
        sport.clear();
        for(V2AdviceDetailData DetailData:weiyundong_list){
            sport.add(DetailData);
        }
        for(V2AdviceDetailData DetailData:gongfa_list){
            sport.add(DetailData);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rcYundongtuijian.setLayoutManager(layoutManager);
        V2HealthAdviceAdapter yinshi_adapter = new V2HealthAdviceAdapter(sport);
        binding.rcYundongtuijian.setAdapter(yinshi_adapter);

        JSONArray array= JSONArray.parseArray(JSON.toJSONString(sport));
        UserSPUtils.putUserString(getActivity(), FileConstants.YunDong_data,JSON.toJSONString(array));
        UserSPUtils.putUserString(getActivity(), FileConstants.HasReport,"true");
    }


    /**
     * description: 显示足浴养生信息
     * @param data 养生详细数据
     * @author visitant
     * @time 2021/1/6 11:48
     */
    public void showZuYuView(V2HealthAdviceData data) {
        List<V2AdviceDetailData> bath=data.getBath();
        ArrayList<V2AdviceDetailData> zuyu_list=new ArrayList();
        for(V2AdviceDetailData DetailData :bath ){
            if(DetailData.getCategoryName().equals("足浴注意")){
                if(TextUtils.isEmpty(DetailData.getContent())){
                    binding.rlZuyuNotice.setVisibility(GONE);
                }else{
                    binding.tvZuyuNotice.setText("\u3000\u3000"+DetailData.getContent());
                    binding.tvZuyuNotice.setVisibility(View.VISIBLE);
                }
            }else if(DetailData.getCategoryName().equals("足浴")){
                zuyu_list.add(DetailData);
            }
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rcZuyutuijian.setLayoutManager(layoutManager);
        V2HealthAdviceAdapter zuyu_adapter = new V2HealthAdviceAdapter(zuyu_list);
        binding.rcZuyutuijian.setAdapter(zuyu_adapter);

        JSONArray array= JSONArray.parseArray(JSON.toJSONString(zuyu_list));
        UserSPUtils.putUserString(getActivity(), FileConstants.Zuyu_data,JSON.toJSONString(array));
        UserSPUtils.putUserString(getActivity(), FileConstants.HasReport,"true");
    }

    /**
     * description: 显示理疗养生信息
     * @param data 养生详细数据
     * @author visitant
     * @time 2021/1/6 11:49
     */
    public void showliLiaoView(V2HealthAdviceData data) {
        List<V2AdviceDetailData> physiotherapy=data.getPhysiotherapy();
        ArrayList<V2AdviceDetailData> anmo_list=new ArrayList();
        ArrayList<V2AdviceDetailData> guasha_list=new ArrayList();
        for(V2AdviceDetailData DetailData :physiotherapy ){
            if(DetailData.getCategoryName().equals("理疗注意")){
                if(TextUtils.isEmpty(DetailData.getBeCareful())){
                    binding.rlLiliaoNotice.setVisibility(GONE);
                }else {
                    binding.tvLiliaoNotice.setText("\u3000\u3000" + DetailData.getBeCareful());
                    binding.rlLiliaoNotice.setVisibility(View.VISIBLE);
                }
            }else if(DetailData.getCategoryName().equals("按摩理疗")){
                anmo_list.add(DetailData);
            }else if(DetailData.getCategoryName().equals("刮痧理疗")){
                guasha_list.add(DetailData);
            }
        }
        physiotherapy.clear();
        for(V2AdviceDetailData DetailData:anmo_list){
            physiotherapy.add(DetailData);
        }
        for(V2AdviceDetailData DetailData:guasha_list){
            physiotherapy.add(DetailData);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rcZhongyililiao.setLayoutManager(layoutManager);
        V2HealthAdviceAdapter zuyu_adapter = new V2HealthAdviceAdapter(physiotherapy);
        binding.rcZhongyililiao.setAdapter(zuyu_adapter);

        JSONArray array= JSONArray.parseArray(JSON.toJSONString(physiotherapy));
        UserSPUtils.putUserString(getActivity(), FileConstants.Liliao_data,JSON.toJSONString(array));
        UserSPUtils.putUserString(getActivity(), FileConstants.HasReport,"true");
    }

    /**
     * description: 显示节气养生信息
     * @param data 养生详细数据
     * @author visitant
     * @time 2021/1/6 11:49
     */
    public void showQiJieView(V2HealthAdviceData data) {
        List<V2AdviceDetailData> notice_list=data.getNotice();
        if(notice_list!=null && notice_list.size()>0){
            binding.tvJieqi.setText("\u3000\u3000"+notice_list.get(0).getContent());
        }

    }


////    String id, String CareTitle, String CareType, String CareImageUrl, String AdviceDescribe, String AdviceOperation, String AdviceRole, String AdviceTaboo
//    public void addShowViewData(ArrayList<HealthCareData> HealthCareData_yaoyu,V2AdviceDetailData DetailData){
//            HealthCareData AdDatas = new HealthCareData(DetailData.getYsOnlyId()+ "",
//                    DetailData.getYsName(),
//                    "饮食指导",
//                    DetailData.getImageUrl(),
//                    DetailData.getRawMaterial(),
//                    DetailData.getPractice(),
//                    DetailData.getEffect(),
//                    DetailData.getBeCareful());
//            HealthCareData_yaoyu.add(AdDatas);
//    }


}